package com.kws.tool.service.impl;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.push.model.v20160801.PushRequest;
import com.aliyuncs.push.model.v20160801.PushResponse;
import com.kws.tool.dto.PushDto;
import com.kws.tool.service.AliyunPushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author : zhangbenting
 * @date : 2024/10/24 21:30
 * @Version: 1.0
 */

@Service
public class AliyunPushServiceImpl implements AliyunPushService {

    @Autowired
    private DefaultAcsClient pushClient;

    @Value("${aliyun.push.app-key}")
    private Long appKey;

    @Override
    public String mobilePush(PushDto pushDto) {
        PushRequest request = new PushRequest();
        request.setAppKey(appKey);                            // 设置 AppKey
        request.setTarget(pushDto.getTargetType());           // 推送目标类型，这里设置为设备
        request.setTargetValue(pushDto.getTargetValue());     // 设备标识，可以是设备 ID 或别名
        request.setDeviceType("ALL");                         // 设备类型，ALL 表示所有设备（Android 和 iOS 都可推送）
        request.setPushType(pushDto.getPushType());           // 推送类型，NOTICE 表示通知
        request.setTitle(pushDto.getTitle());                 // 通知标题
        request.setBody(pushDto.getBody());                   // 通知内容
        request.setiOSApnsEnv("DEV");
        try {
            PushResponse response = pushClient.getAcsResponse(request);
            return response.getRequestId();
        } catch (ClientException e) {
            e.printStackTrace();
            return "Error: " + e.getMessage();
        }
    }

}
